package com.lujianfei.filesend.ui.search.adapter

import androidx.databinding.BindingAdapter
import androidx.databinding.ObservableArrayList
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lujianfei.filesend.R
import com.lujianfei.filesend.base.BaseBindingAdapter
import com.lujianfei.filesend.databinding.ItemMessageBinding
import com.lujianfei.filesend.ui.search.model.MessageModel


/**
 * 扫描出来的 tag adapter
 */
class MessageAdapter(items: ObservableArrayList<MessageModel>) :
    BaseBindingAdapter<MessageModel, ItemMessageBinding>(items) {

    var onItemClick:((MessageModel)->Unit)?= null

    override fun getLayoutResId() = R.layout.item_message

    override fun onBindItem(binding: ItemMessageBinding?, bean: MessageModel, position: Int) {
        binding?.root?.setOnClickListener {
            onItemClick?.invoke(bean)
        }
        binding?.model = bean
    }
}

@BindingAdapter(value=["MessageAdapter_bindlist","MessageAdapter_onItemClick"], requireAll = false)
fun MessageAdapter_bindlist(view: RecyclerView,
                            list:ObservableArrayList<MessageModel>,
                            onItemClick:((MessageModel)->Unit)?= null,
) {
    if (view.adapter != null) {
        return
    }
    val adapter = MessageAdapter(list)
    adapter.onItemClick = onItemClick
    view.adapter = adapter
    val linearLayoutManager = LinearLayoutManager(view.context)
    linearLayoutManager.reverseLayout = true
    linearLayoutManager.stackFromEnd = true
    view.layoutManager = linearLayoutManager
}